package cn.iocoder.an.module.mold.controller.admin.upkeepcategory;

import cn.iocoder.an.module.mold.controller.admin.category.vo.CategoryImportExcelVO;
import cn.iocoder.an.module.mold.controller.admin.category.vo.CategoryListReqVO;
import cn.iocoder.an.module.mold.controller.admin.category.vo.CategoryRespVO;
import cn.iocoder.an.module.mold.controller.admin.upkeepitem.vo.UpkeepItemRespVO;
import cn.iocoder.an.module.mold.dal.dataobject.category.CategoryDO;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepitem.UpkeepItemDO;
import cn.iocoder.an.module.mold.service.upkeepitem.UpkeepItemService;
import cn.iocoder.an.module.mold.utils.ImportRespVO;
import com.fhs.core.trans.anno.TransMethodResult;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.upkeepcategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcategory.UpkeepCategoryDO;
import cn.iocoder.an.module.mold.service.upkeepcategory.UpkeepCategoryService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 保养类型")
@RestController
@RequestMapping("/mold/upkeep-category")
@Validated
public class UpkeepCategoryController {

    @Resource
    private UpkeepCategoryService upkeepCategoryService;

    @Resource
    private UpkeepItemService upkeepItemService;

    @PostMapping("/create")
    @Operation(summary = "创建保养类型")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-category:create')")
    public CommonResult<Long> createUpkeepCategory(@Valid @RequestBody UpkeepCategorySaveReqVO createReqVO) {
        return success(upkeepCategoryService.createUpkeepCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新保养类型")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-category:update')")
    public CommonResult<Boolean> updateUpkeepCategory(@Valid @RequestBody UpkeepCategorySaveReqVO updateReqVO) {
        upkeepCategoryService.updateUpkeepCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除保养类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:upkeep-category:delete')")
    public CommonResult<Boolean> deleteUpkeepCategory(@RequestParam("id") Long id) {
        upkeepCategoryService.deleteUpkeepCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除保养类型")
                @PreAuthorize("@ss.hasPermission('mold:upkeep-category:delete')")
    public CommonResult<Boolean> deleteUpkeepCategoryList(@RequestParam("ids") List<Long> ids) {
        upkeepCategoryService.deleteUpkeepCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得保养类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-category:query')")
    @TransMethodResult
    public CommonResult<UpkeepCategoryRespVO> getUpkeepCategory(@RequestParam("id") Long id) {
        UpkeepCategoryDO upkeepCategory = upkeepCategoryService.getUpkeepCategory(id);
        return success(BeanUtils.toBean(upkeepCategory, UpkeepCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得保养类型分页")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-category:query')")
    @TransMethodResult
    public CommonResult<PageResult<UpkeepCategoryRespVO>> getUpkeepCategoryPage(@Valid UpkeepCategoryPageReqVO pageReqVO) {
        PageResult<UpkeepCategoryDO> pageResult = upkeepCategoryService.getUpkeepCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UpkeepCategoryRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得保养类型列表")
    public CommonResult<List<UpkeepCategoryRespVO>> getUpkeepCategoryList() {
        List<UpkeepCategoryDO> list = upkeepCategoryService.getUpkeepCategoryAll();
        return success(BeanUtils.toBean(list, UpkeepCategoryRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出保养类型 Excel")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUpkeepCategoryExcel(@Valid UpkeepCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UpkeepCategoryDO> list = upkeepCategoryService.getUpkeepCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "保养类型.xls", "数据", UpkeepCategoryRespVO.class,
                        BeanUtils.toBean(list, UpkeepCategoryRespVO.class));
    }




    @GetMapping("/get-import-template")
    @Operation(summary = "获得保养类型导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<UpkeepCategoryImportExcelVO> list = Arrays.asList(
                UpkeepCategoryImportExcelVO.builder()
                        .name("导入保养")
                        .code("MH-01")
                        .status(1)
                        .accountMethod(1)
                        .upkeepPeriod(1l)
                        .tobePeriod(1l)
                        .reminderPeriod(1l)
                        .description("描述内容")
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "模具保养类型导入模板.xlsx", "列表", UpkeepCategoryImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入图号")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<UpkeepCategoryImportExcelVO> list = ExcelUtils.read(file, UpkeepCategoryImportExcelVO.class);
        return success(upkeepCategoryService.importUpkeepCategoryList(list, updateSupport));
    }



}